/**
* @file notify.cpp
*
* Copyright (c) Huawei Technologies Co., Ltd. 2025. All Rights reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "runtime/acl_rt_impl.h"

#include "runtime/event.h"
#include "runtime/rts/rts_event.h"

#include "log_inner.h"
#include "error_codes_inner.h"
#include "toolchain/resource_statistics.h"

aclError aclrtCreateNotifyImpl(aclrtNotify *notify, uint64_t flag)
{
    ACL_LOG_INFO("start to execute aclrtCreateNotify, flag is [%lu]", flag);
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(notify);

    const rtError_t rtErr = rtsNotifyCreate(static_cast<rtNotify_t*>(notify), flag);
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("call rtsNotifyCreate failed, runtime result = %d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }

    ACL_LOG_INFO("successfully execute aclrtCreateNotify");
    return ACL_SUCCESS;
}

aclError aclrtDestroyNotifyImpl(aclrtNotify notify)
{
    ACL_LOG_INFO("start to execute aclrtDestroyNotify");
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(notify);

    const rtError_t rtErr = rtsNotifyDestroy(static_cast<rtNotify_t>(notify));
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("call rtsNotifyDestroy failed, runtime result = %d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }

    ACL_LOG_INFO("successfully execute aclrtDestroyNotify");
    return ACL_SUCCESS;
}

aclError aclrtRecordNotifyImpl(aclrtNotify notify, aclrtStream stream)
{
    ACL_LOG_INFO("start to execute aclrtRecordNotify");
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(notify);

    const rtError_t rtErr = rtsNotifyRecord(static_cast<rtNotify_t>(notify), static_cast<rtStream_t>(stream));
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("call rtsNotifyRecord failed, runtime result = %d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }

    ACL_LOG_INFO("successfully execute aclrtRecordNotify");
    return ACL_SUCCESS;
}

aclError aclrtWaitAndResetNotifyImpl(aclrtNotify notify, aclrtStream stream, uint32_t timeout)
{
    ACL_LOG_INFO("start to execute aclrtWaitAndResetNotify, timeout is [%u] ms", timeout);
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(notify);

    const rtError_t rtErr = rtsNotifyWaitAndReset(static_cast<rtNotify_t>(notify),
        static_cast<rtStream_t>(stream), timeout);
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("call rtsNotifyWaitAndReset failed, runtime result = %d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }

    ACL_LOG_INFO("successfully execute aclrtWaitAndResetNotify");
    return ACL_SUCCESS;
}

aclError aclrtGetNotifyIdImpl(aclrtNotify notify, uint32_t *notifyId)
{
    ACL_LOG_INFO("start to execute aclrtGetNotifyId");
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(notify);
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(notifyId);

    const rtError_t rtErr = rtsNotifyGetId(static_cast<rtNotify_t>(notify), notifyId);
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("call rtsNotifyGetId failed, runtime result = %d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }

    ACL_LOG_INFO("successfully execute aclrtGetNotifyId");
    return ACL_SUCCESS;
}
